<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm" :inline="true" :model="queryForm">
        <el-form-item :label="dataList.BorrowId" prop="borrowId">
          <el-input v-model="queryForm.borrowId" placeholder="借用单Id" />
        </el-form-item>
        <el-form-item :label="dataList.DeviceNumber" prop="num">
          <el-input
            v-model="queryForm.num"
            :placeholder="dataList.DeviceNumber"
          />
        </el-form-item>
        <el-form-item :label="dataList.PreUseStatus" prop="beforeStatus">
          <el-input
            v-model="queryForm.beforeStatus"
            :placeholder="dataList.PreUseStatus"
          />
        </el-form-item>
        <el-form-item :label="dataList.PreUseStatus" prop="beforeStatusName">
          <el-input
            v-model="queryForm.beforeStatusName"
            :placeholder="dataList.PreUseStatus"
          />
        </el-form-item>
        <el-form-item :label="dataList.StorageLocation" prop="storageAddress">
          <el-input
            v-model="queryForm.storageAddress"
            :placeholder="dataList.StorageLocation"
          />
        </el-form-item>
        <el-form-item :label="dataList.BorrowStatus" prop="borrowStatus">
          <el-input
            v-model="queryForm.borrowStatus"
            :placeholder="dataList.BorrowStatus"
          />
        </el-form-item>
        <el-form-item :label="dataList.BorrowStatus" prop="borrowStatusName">
          <el-input
            v-model="queryForm.borrowStatusName"
            :placeholder="dataList.BorrowStatus"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">{{ btn.Query }}</el-button>
          <el-button @click="cancelForm">{{ btn.Clear2 }}</el-button>
        </el-form-item>
      </el-form>

      <div class="base-content-operate">
        <el-button icon="el-icon-plus" type="primary" @click="onNew">{{
          btn.Add
        }}</el-button>
      </div>

      <el-table
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
      >
        <el-table-column align="center" :label="dataList.Number" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.BorrowId">
          <template slot-scope="scope">
            {{ scope.row.borrowId }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DeviceNumber">
          <template slot-scope="scope">
            {{ scope.row.num }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.PreUseStatus">
          <template slot-scope="scope">
            {{ scope.row.beforeStatus }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.PreUseStatus">
          <template slot-scope="scope">
            {{ scope.row.beforeStatusName }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.StorageLocation">
          <template slot-scope="scope">
            {{ scope.row.storageAddress }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.BorrowStatus">
          <template slot-scope="scope">
            {{ scope.row.borrowStatus }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.BorrowStatusName">
          <template slot-scope="scope">
            {{ scope.row.borrowStatusName }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.Operation">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)"
                >{{ btn.Edit }}</el-link
              >
              <el-link
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >{{ btn.Delete }}</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :current-page="pagination.pageIndex"
        :page-count="pagination.total"
        @changePage="changePage"
        @sizeChange="sizeChange"
      />
    </div>

    <DeviceBorrowInfoEdit
      ref="DeviceBorrowInfoEdit"
      :dialog-form-visible.sync="dialogFormVisible"
    />
  </div>
</template>

<script>
import { deleteRecord, getList } from "../../api/DeviceMaintenanceInfo.js";
import DeviceBorrowInfoEdit from "./DeviceBorrowInfoEdit.vue";

export default {
  name: "DeviceBorrowInfoList",
  components: { DeviceBorrowInfoEdit },
  data() {
    return {
      loading: true,
      list: [],
      queryForm: {
        borrowId: 0,
        num: "",
        beforeStatus: 0,
        beforeStatusName: "",
        storageAddress: "",
        borrowStatus: 0,
        borrowStatusName: "",
        departId: sessionStorage.getItem("currentDepartmentId"),
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  computed: {
    btn() {
      return this.$t("ModuleDevice.buttons");
    },
    dataList() {
      return this.$t("ModuleDevice.Device");
    },
  },
  created() {
    this.getList();
  },
  methods: {
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.pagination.pageIndex = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.$refs.DeviceBorrowInfoEdit.formData = this.Helper.deepCopy(
        this.$refs.DeviceBorrowInfoEdit.initData
      );
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(rowData) {
      this.$refs.DeviceBorrowInfoEdit.formData = rowData;
      this.dialogFormVisible = true;
    },
    onQuery() {
      this.pagination.pageIndex = 1;
      this.getList();
    },
  },
};
</script>
